window.onload = function() {
	init();
	pineza.boardHTML = {};
}

function leaveNote(board) {
	/* Access the `board' frame and its properties */
	var bf = parent.document.getElementById('board');
	if ((!bf) || (!bf.contentWindow) || (!bf.contentWindow.pineza)) {
		alert('Cannot access "board" iframe or its properties');
		return false;
	}

	var bfd = bf.contentWindow.document;
	if (!bfd) {
		alert('Cannot access "board" iframe documnet');
		return false;
	}

	var nl = parent.pineza.pickedNotes;
	var x = '';
	for (var i in nl) {
		x += i + '@';
	}

	if (x) {
		if (pineza.login) {
			if (!isSet(bf.contentWindow.pineza.board)) {
				alert('Cannot access source board');
				return false;
			}

			var apo = bf.contentWindow.pineza.board;
			if (apo != board) {
				delete parent.pineza.pickedNotes;
				parent.pineza.pickedNotes = new Array();
				workingBoardSB(apo);
				workingBoardSB(board);
				delete pineza.boardHTML[apo];
				delete pineza.boardHTML[board];
				for (var i in nl) {
					var nt = bfd.getElementById('n' + i);
					if (nt && nt.parentNode) {
						nt.parentNode.removeChild(nt);
					}
				}

				var req = newRequest('moveNotes');
				req.open('post', pineza.server + 'board/moveNotes.php', true);
				req.onreadystatechange = function() {
					checkMoveNotes(req, apo, board);
					bf.contentWindow.refreshBoardHTML(apo);
				};
				var params = 'login=' + uri(pineza.login) + '&notes=' + uri(x) +
					'&board=' + uri(board);
				sendRequest(req, params);
			}
		}
		else {
			alert('Plase login in order to move notes between boards.');
		}
	}
	else {
		if ((board == 'TRASH') && confirm('Empty trash?')) {
			emptyTrash();
			return false;
		}

		x = bfd.getElementById('boardNotes');
		if (!x) return refreshBoard(board, bf);

		var b = x.parentNode;
		if (!b) return refreshBoard(board, bf);

		b.style.backgroundImage = (board ? "url('" + pineza.server +
			"images/tsoxes/" + board + ".jpg')" : '');
		setTimeout(function() {
			refreshNotes(board, x, bf);
		}, 10);
	}

	return false;
}

function refreshBoard(board, bf) {
	bf.src = pineza.server + 'board/index.php?board=' +
		uri(board) + '&epilogi=yes';
	return false;
}

function refreshNotes(board, div, bf) {
	if (isSet(pineza.boardHTML[board])) {
		div.innerHTML = pineza.boardHTML[board];
		bf.contentWindow.pineza.board = board;
		return;
	}

	div.innerHTML = '<img src="' + pineza.server +
		'images/working.gif" alt="" style="width: 0.8cm;" />' +
		'<span style="font-style: italic;">Loading notes. Please wait…</span>';
	var req = newRequest('boardNotes');
	req.open('post', pineza.server + 'board/boardNotes.php', true);
	req.onreadystatechange = function() {
		boardNotesCheck(req, board, div, bf);
	};
	var params = 'login=' + uri(pineza.login) + '&board=' + uri(board);
	sendRequest(req, params);
}

function boardNotesCheck(req, board, div, bf) {
	if (req.readyState != 4)
		return;

	if (req.status != 200) {
		alert('boardNotesCheck: AJAX request failed (status = ' + req.status + ')');
		return;
	}

	var rsp = fixResponse(req);
	if (rsp.match(/^ERROR@/)) {
		rsp = rsp.replace(/^ERROR@/, '');
		refreshBoard(board, bf);
	}
	else {
		div.innerHTML = rsp;
		pineza.boardHTML[board] = rsp;
		bf.contentWindow.pineza.board = board;
	}
}

function checkMoveNotes(req, apo, sto) {
	if (req.readyState != 4)
		return;

	if (req.status != 200) {
		alert('checkMoveNotes: AJAX request failed (status = ' + req.status + ')');
		return;
	}

	refreshBoardSB(sto);
	refreshBoardSB(apo);
}

function clickTitlosPinaka(e, board) {
	if (!e) e = window.event;
	if (e.stopPropagation) {
		e.stopPropagation();
	} else {
		e.cancelBubble = true;
	} 
}

function setBoardTitle(b, title) {
	title = (title === undefined ? '' : title);
	b.title = (title ? title + '. ' : '');
	var nl = parent.pineza.pickedNotes;
	for (var i in nl) {
		b.title += 'Click to move selected notes';
		return false;
	}

	b.title += 'Click to see the notes';
	return false;
}

function editPinakasTitle(board) {
	var x = getelid('pt' + board);
	if (x && x.style) {
		if (x.style.display == 'inline') {
			x.style.display = 'none';
		}
		else {
			x.style.display = 'inline';
			x.focus();
		}
	}
}

function onomaPinakaUpdate(e, board) {
	if (window.event) {
		var key = e.keyCode;
	}
	else if(e.which) {
		key = e.which;
	}
	if (key != 13) return;

	var x = getelid('pt' + board);
	if (!x) return;

	var req = newRequest('titleUpdate');
	req.open('post', pineza.server + 'boardSB/titleUpdate.php', true);
	req.onreadystatechange = function() {
		titleUpdateCheck(req, board);
	};
	var params = 'login=' + uri(pineza.login) + '&title=' + uri(x.value) +
		'&board=' + uri(board);
	sendRequest(req, params);
	x.style.display = 'none';
}

function titleUpdateCheck(req, board) {
	if (req.readyState != 4)
		return;

	if (req.status != 200) {
		alert('titleUpdateCheck: AJAX request failed (status = ' + req.status + ')');
		return;
	}

	var x = parent.document.getElementById('boardSB');
	x.src = x.src;
}

function emptyTrash() {
	var req = newRequest('emptyTrash');
	req.open('post', pineza.server + 'boardSB/emptyTrash.php', true);
	req.onreadystatechange = function() {
		if (req.readyState != 4)
			return;

		if (req.status != 200) {
			alert('empytTrash: AJAX request failed (status = ' + req.status + ')');
			return;
		}

		var x = parent.document.getElementById('boardSB');
		x.src = x.src;
	};
	var params = 'login=' + uri(pineza.login);
	sendRequest(req, params);
}
